package com.tencent.iov.cowin.befsale.clue.controller.clue.assembler;

import com.software.boot.common.exception.BusinessException;
import com.software.boot.common.exception.NEError;
import com.software.boot.common.model.dao.BaseAssembler;
import com.software.boot.common.model.rest.BaseRequest;
import com.software.boot.common.model.rest.BaseResponse;
import com.software.boot.common.util.EmptyUtils;
import com.software.common.config.SearchLocalHeader;
import com.software.common.enums.SCRMRequestHeaderEnum;
import com.tencent.iov.cowin.befsale.clue.controller.cus.vo.request.AddCustomerFollowRecordRequest;
import com.tencent.iov.cowin.befsale.clue.controller.cus.vo.request.ClueIntentionGradeDataRequest;
import com.tencent.iov.cowin.befsale.clue.controller.cus.vo.request.UpdateClueEmpRequest;
import com.tencent.iov.cowin.befsale.clue.domain.clue.po.Clue;
import com.tencent.iov.cowin.befsale.clue.domain.clue.po.ClueFail;
import com.tencent.iov.cowin.befsale.clue.value.enums.*;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;

/**
 * @Author: whp
 * @Date: 2021-12-21 16:16:06
 * @Description: 线索信息表适配器，负责vo<->entity、vo<->po的转换和必要的参数检查，行为
 */
@Slf4j
public class ClueAssembler extends BaseAssembler<BaseResponse, BaseRequest, Clue> {

    public static Clue initUpdateClue(Clue request) {
        request.setUpdatedBy(SearchLocalHeader.getHeader(SCRMRequestHeaderEnum.HEADER_EMP_ID.getCode()));
        request.setUpdatedName(SearchLocalHeader.getHeader(SCRMRequestHeaderEnum.HEADER_EMP_NAME.getCode()));
        return request;
    }

    public static Clue initUpdateByDistribute(UpdateClueEmpRequest request) {
        Clue clue = new Clue();
        clue.setIds(request.getClueIds());
        clue.setDistributeTime(new Date());
        clue.setStatus(ClueStatsEnum.FOLLOW_UP.getCode());
        clue.setDealerFollowStatus(DealerFollowStatusEnum.DISTRIBUTION_ADVISER.getCode());
        clue.setUpdatedBy(SearchLocalHeader.getHeader(SCRMRequestHeaderEnum.HEADER_EMP_ID.getCode()));
        clue.setUpdatedName(SearchLocalHeader.getHeader(SCRMRequestHeaderEnum.HEADER_EMP_NAME.getCode()));
        return clue;
    }

    public static void initUpdateByFailClue(UpdateClueEmpRequest request, ClueFail clueFail, Clue clue) {
        clue.setId(request.getClueId());
        clue.setUpdatedBy(SearchLocalHeader.getHeader(SCRMRequestHeaderEnum.HEADER_EMP_ID.getCode()));
        clue.setUpdatedName(SearchLocalHeader.getHeader(SCRMRequestHeaderEnum.HEADER_EMP_NAME.getCode()));

        FailStatusEnum failStatusEnum = FailStatusEnum.getByCode(request.getFailStatus());
        switch (failStatusEnum) {
            case AGREE:
                clue.setStatus(ClueStatsEnum.IS_FAIL.getCode());
                clue.setIsActive(IsActiveEnum.ACTIVE_NO.getCode());
                break;
            case ACTIVATE:
                clue.setDistributeTime(new Date());
                clue.setDealerFollowStatus(DealerFollowStatusEnum.DISTRIBUTION_ADVISER.getCode());
                clue.setStatus(clueFail.getClueStatus());
                clue.setIsActive(IsActiveEnum.ACTIVE_YES.getCode());
                break;
            case REFUSE:
                clue.setStatus(clueFail.getClueStatus());
                break;
            default:
                break;
        }
    }

    public static Clue initUpdateByFollowInfo(AddCustomerFollowRecordRequest recordRequest, Clue existClue) {
        Long clueId = recordRequest.getClueId();
        Clue clue = new Clue();
        clue.setId(clueId);
        if (EmptyUtils.isEmpty(recordRequest.getFollowContent())) {
            return null;
        }
        FollowContentEnum followContentEnum = FollowContentEnum.getByCode(Integer.parseInt(recordRequest.getFollowContent()));
        if (EmptyUtils.isEmpty(followContentEnum)) {
            throw new BusinessException(NEError.BUSINESS_ERROR, "跟进内容不存在");
        }
        switch (followContentEnum) {
            case PRODUCT_INTRO:
                if (existClue.getStatus() == null || existClue.getStatus().equals(ClueStatsEnum.FOLLOW_UP.getCode())) {
                    clue.setStatus(ClueStatsEnum.FOLLOW_UP.getCode());
                }
                break;
            case CHECKIN:
            case DRIVE_TEST:
                clue.setStatus(ClueStatsEnum.CHECKIN.getCode());
                break;
            case ORDER:
                clue.setIntentionGrade(existClue.getIntentionGrade());
                clue.setStatus(ClueStatsEnum.ORDER.getCode());
                clue.setIsActive(IsActiveEnum.ACTIVE_NO.getCode());
                break;
            case DELIVERY:
                clue.setStatus(ClueStatsEnum.DELIVERY.getCode());
                clue.setIsActive(IsActiveEnum.ACTIVE_NO.getCode());
                break;
            default:
                break;
        }
        return clue;
    }

    public static ClueIntentionGradeDataRequest initClueIntentionGradeDataRequest(ClueIntentionGradeDataRequest request) {
        request.setTenantId(SearchLocalHeader.getHeader(SCRMRequestHeaderEnum.HEADER_TENANTID.getCode()));
        request.setCorpId(SearchLocalHeader.getHeader(SCRMRequestHeaderEnum.HEADER_CORP_ACCOUNTID.getCode()));
        request.setDealerId(SearchLocalHeader.getHeader(SCRMRequestHeaderEnum.HEADER_DEALER_ID.getCode()));
        return request;
    }
}
